// pages/sales/word_art/word_art_detail.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    isEditOrAdd: true,
    wordArtCnt:"",
    keywords:"",
    mWordArtCnt:"",
    modalKeyword:false,
    mKeywordArr:[],
    mKeyword:"",
    iscust:0,
    pageSize: 15,
    curPage: 1,
    total:0,
    wordArtList:[],
    isEditState:false,
    isLoading:false,
    isBottom: false
  },
  /**
   * 编辑自定义话术
   */
  toEditWordArt(e){

    let { id,value,iscust } = e.currentTarget.dataset
    this.setData({
      isEditState: true,
      isEditOrAdd:false,
      id:id,
      mWordArtCnt:value,
      wordArtCnt:value,
      iscust:iscust
    });
    this.fetchDetail(id);
  },
  /**
   * 新增话术
   */
  toAddWordArt(){
    this.setData({
      isEditState: true,
      isEditOrAdd:true,
      mWordArtCnt:'',
      id:''
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var ctx = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.fetchRecentOrCustWordArt(true);
      if(options.id){
        ctx.setData({
          isEditOrAdd: false,
          id: options.id,
          iscust: options.iscust
        });
        ctx.fetchDetail(options.id);
      }else{
        ctx.setData({
          isEditOrAdd: true
        });
        
      }
    });
  },
  fetchDetail(id){
    var ctx = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/quickReply/findOne",
      data: {
        cusmallToken: cusmallToken,
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          ctx.setData({
            wordArtCnt: data.model.record.content,
            keywords: data.model.record.keywords
          });
        }
      },
      fail() {

      },
      complete() {
        wx.hideLoading();
      }
    });
  },

  wordArtInput(e){
    var str = e.detail.value;
    this.setData({
      mWordArtCnt: str
    });
  },
  editWordArt(){
    var ctx = this;
    ctx.setData({
      isEditOrAdd: true,
      mWordArtCnt: ctx.data.wordArtCnt,
      mKeywordArr: ctx.data.keywords.split(",")
    });
  },
  delWordArt(){
    var ctx = this;
    ctx.setData({
      wordArtList:[],
      isBottom:false,
      curPage:1,
      isLoading:false,
      total:0,
    });
    wx.showModal({
      title:"提示",
      content:"是否删除",
      success(res){
        if(res.confirm){
          
          wx.showLoading({
            title: '删除中',
          });
          wx.request({
            url: cf.config.pageDomain + "/applet/mobile/visitcard/quickReply/delete",
            data: {
              cusmallToken: cusmallToken,
              id: ctx.data.id
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.hideLoading()
              var data = res.data;
              if (data && 0 === data.ret) {
                let pages = getCurrentPages();//当前页面
                let prevPage = pages[pages.length - 2];//上一页面
                let retData = {
                  isNeedReload: true
                };
                //直接给上移页面赋值
                prevPage.setData(retData);
                wx.showToast({
                  title: '删除成功',
                  icon: "none",
                  success(){
                   /* setTimeout(function(){
                      wx.navigateBack({
                        delta: 1
                      });
                    },800)*/
                    ctx.fetchRecentOrCustWordArt(true);
                    ctx.setData({
                      isEditState:false
                    })
                  }
                });
              }else{
                wx.showToast({
                  title: data.msg,
                  icon: "none"
                });
              }
            },
            fail() {

            },
            complete() {
            }
          });
        }
      }
    });

  },
  /**
   * 添加 关键字
   */
  keywordHandle(e){
    console.log(e);
    var keyword = e.detail.value.inputKeyword;
    if ("" === keyword){
      wx.showToast({
        title: '关键字不能为空',
        icon:"none"
      })
      return;
    }
    var kwArr = this.data.mKeywordArr;
    kwArr.push(keyword);
    this.setData({
      modalKeyword: false,
      mKeywordArr: kwArr,
      mKeyword:""
    });
  },
  delKeyword(e){
    var idx = e.currentTarget.dataset.idx;
    var kwArr = this.data.mKeywordArr;
    kwArr.splice(idx, 1);
    this.setData({
      mKeywordArr:kwArr
    });
  },
  showModal(){
    this.setData({
      modalKeyword: true
    });
  },
  hideModal(){
    this.setData({
      modalKeyword: false
    });
  },
  closeMask(){
    this.setData({
      isEditState:false
    });
  },
  addOrEdit(){
    var ctx = this;
    if ("" === ctx.data.mWordArtCnt) {
      wx.showToast({
        title: '话术内容不能为空',
        icon: "none"
      });
      return;
    }
    // if (0 === ctx.data.mKeywordArr.length) {
    //   wx.showToast({
    //     title: '关键字不能为空',
    //     icon: "none"
    //   });
    //   return;
    // }
    var params = {
      cusmallToken: cusmallToken,
      content: ctx.data.mWordArtCnt,
      keywords: ctx.data.mKeywordArr.join(","),

    }

    ctx.setData({
      wordArtList:[],
      isBottom:false,
      curPage:1,
      isLoading:false,
      total:0,
    });
    var reqUrl = "/applet/mobile/visitcard/quickReply/add";//添加
    if (ctx.data.id){
      reqUrl ="/applet/mobile/visitcard/quickReply/update";//更新
      params.id = ctx.data.id;
    }else{
      params.eid = wx.getStorageSync("employee").id;
    }
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: cf.config.pageDomain + reqUrl,
      data: params,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          wx.showToast({
            title: '保存成功',
            icon: "none",
          });
          let pages = getCurrentPages();//当前页面
          let prevPage = pages[pages.length - 2];//上一页面
          let retData = {
            isNeedReload : true
          };
          //直接给上移页面赋值
          prevPage.setData(retData);
          ctx.fetchRecentOrCustWordArt(true);
          ctx.setData({
            isEditState:false
          });
          //setTimeout(function () {
            //wx.navigateBack({
             // delta: 1
            //});
          //}, 1000)
        }else{
          wx.showToast({
            title: data.msg,
            icon: "none"
          });
        }
      },
      fail() {

      },
      complete() {
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))